package com.kcl.dfss;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kcl.dfss.http.CameraConnection;
import com.kcl.dfss.http.CameraConnection.ResponseHandler;

public class SetDeviceNameActivity extends Activity {

	protected static final String tag = "SetDeviceNameActivity";
	private TextView tv_savedname;
	private EditText edit_dname;
	private ImageView dname_clear1;
	private LinearLayout devicename_back;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_set_device_name);
		
		tv_savedname = (TextView) findViewById(R.id.tv_savedname);
		edit_dname = (EditText) findViewById(R.id.edit_dname);
		dname_clear1 = (ImageView) findViewById(R.id.dname_clear1);
		devicename_back = (LinearLayout) findViewById(R.id.devicename_back);
		
		Intent intent = getIntent();
		String devicename = intent.getStringExtra("devicename");
		edit_dname.setText(devicename);
		
		final CameraConnection cameraConnection = new CameraConnection(new ResponseHandler(){

			@Override
			public void handleResponse(int request_code, String response) {
				Log.d(tag, "修改名称response:"+response);
				Log.d(tag, "修改名称request_code:"+request_code);
				switch(request_code){
				case CameraConnection.CODE_SET_TIME:
					if(response.substring(0, 2).equals("OK")){
						Toast.makeText(SetDeviceNameActivity.this, "修改热点名称成功，重启设备后生效", Toast.LENGTH_SHORT).show();
						finish();
					}else{
						Toast.makeText(SetDeviceNameActivity.this, "修改热点名称失败", Toast.LENGTH_SHORT).show();
						finish();
					}
					break;
				}
			}}, this);
		
		dname_clear1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				edit_dname.getText().clear();
			}
		});
		
		devicename_back.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		tv_savedname.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String devicename = edit_dname.getText().toString();
				cameraConnection.setSSID(devicename);
			}
		});
	}

}
